package com.framework.core;

import android.os.Bundle;
import android.support.annotation.ColorRes;
import android.support.annotation.DrawableRes;
import android.support.annotation.IdRes;
import android.support.annotation.Nullable;
import android.support.annotation.StringRes;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;

import com.framework.core.widget.NoneSlidingConflictsLinearLayout;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * @author shenmengchao
 * @version 1.0.0
 * @date 2017/7/18
 * @description
 */

public abstract class TitleBaseActivity extends BaseActivity {

    private NoneSlidingConflictsLinearLayout mContainerLayout;
    private ViewGroup mContentView;
    protected SwipeRefreshLayout mSwipeRefresh;
    protected Toolbar mToolbar;
    protected View mVSeparator;
    protected TextView mRightText;
    private EditText mEditText;
    private Unbinder mUnbinder;
//    private ActivityComponent mActivityComponent;
//    private ActivityHelper mActivityHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
//        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
//        setComponent();
        super.onCreate(savedInstanceState);
        if (isFullScreen()) {
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                    WindowManager.LayoutParams.FLAG_FULLSCREEN);
        }
        setContentView(useSwipeRefreshLayout() ? R.layout.base_container_add_refresh_layout : R.layout.base_container_layout);
        mContainerLayout = (NoneSlidingConflictsLinearLayout) super.findViewById(R.id.ll_container);
        mContentView = (ViewGroup) getLayoutInflater().inflate(inflateContentView(), mContainerLayout, false);
        if (useSwipeRefreshLayout()) {
            mSwipeRefresh = (SwipeRefreshLayout) super.findViewById(R.id.refresh);
            mSwipeRefresh.setColorSchemeResources(R.color.colorAccent);
            mSwipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
                @Override
                public void onRefresh() {
                    refreshData();
                }
            });
        }
        mUnbinder = ButterKnife.bind(this, mContentView);
        mToolbar = ((Toolbar) super.findViewById(R.id.toolbar));
        mVSeparator = super.findViewById(R.id.v_separator);
        if (null != mToolbar) {
            setSupportActionBar(mToolbar);
            mToolbar.setNavigationIcon(R.mipmap.ic_back);
            mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onBackPressed();
                }
            });
        }

//        this.mActivityHelper = new ActivityHelperImpl(mContainerLayout, this, mContentView);
        /** 初始化 activity */
        initInstanceState(savedInstanceState);
        initActivity(mContainerLayout);
    }

    @Override
    public View findViewById(@IdRes int id) {
        return mContentView.findViewById(id);
    }

    public void dismissToolbar() {
        mToolbar.setVisibility(View.GONE);
        mVSeparator.setVisibility(View.GONE);
    }

    public void dismissSeparator() {
        mVSeparator.setVisibility(View.GONE);
    }

    public boolean isFullScreen() {
        return false;
    }

    /**
     * 初始化注入器
     */
//    protected void setComponent() {
//        mActivityComponent = DaggerActivityComponent.builder()
//                .applicationComponent(XKApplicationLike.getInstance().component())
//                .activityModule(new ActivityModule(this))
//                .build();
//        inject(mActivityComponent);
//    }
    public abstract void initActivity(View view);

//    public abstract void inject(ActivityComponent pActivityComponent);

    /**
     * 得到dagger的component的实例
     *
     * @return
     */
//    public ActivityComponent getActivityComponent() {
//        return mActivityComponent;
//    }

    /**
     * @return 设置content layout id
     */
    public abstract int inflateContentView();

    /**
     * 初始化保存状态
     *
     * @param savedInstanceState
     */
    protected void initInstanceState(Bundle savedInstanceState) {
    }

    public boolean useSwipeRefreshLayout() {
        return false;
    }

    protected void refreshData() {
    }


    public void setHeaderTitle(@StringRes int resId) {
        if (null != mToolbar) {
            mToolbar.setTitle(getResources().getString(resId));
        }
    }

    public void setHeaderTitle(String pTitleStr) {
        if (null != mToolbar) {
            mToolbar.setTitle(pTitleStr);
        }
    }

    public void setHeaderTitleColor(int color) {
        if (null != mToolbar) {
            mToolbar.setTitleTextColor(getResources().getColor(color));
        }
    }

    /**
     * 设置右button内容和点击效果
     *
     * @param pText 右button内容
     * @param pUrl  跳转的url（h5专用）
     *              <p>
     *              created at 2016/3/23 11:13
     * @author LeiYu
     */
    public void showRightTextView(String pText, final String pUrl) {
        mRightText = (TextView) super.findViewById(R.id.tv_right_text);
        mRightText.setVisibility(View.VISIBLE);
        mRightText.setText(pText);
        mRightText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (pUrl != null && !TextUtils.isEmpty(pUrl)) {
                    onRightTextViewClick(pUrl);
                } else {
                    onRightTextViewClick();
                }
            }
        });
    }

//    public void showEditText() {
//        mEditText = (EditText) super.findViewById(R.id.et_center);
//        mEditText.setVisibility(View.VISIBLE);
//    }

    /**
     * 设置右按钮字体颜色
     *
     * @param color
     */
    public void setRightTextColor(@ColorRes int color) {
        mRightText = (TextView) super.findViewById(R.id.tv_right_text);
        mRightText.setTextColor(getResources().getColor(color));
    }

    public void setRightTextClickable(boolean clickable) {
        mRightText = (TextView) super.findViewById(R.id.tv_right_text);
        mRightText.setClickable(clickable);
    }

    public void showRightTextView(String pText) {
        showRightTextView(pText, null);
    }

    public void onRightTextViewClick(String pUrl) {
    }

    public void onRightTextViewClick() {
    }

    public Toolbar getToolbar() {
        return mToolbar;
    }


//    public void commonExceptionDispose(RestError pRestError) {
//        mActivityHelper.commonExceptionDispose(pRestError);
//    }

    public void showContent() {
        if (mContainerLayout.getChildCount() > 0) {
            mContainerLayout.removeAllViews();
        }
        mContainerLayout.addView(mContentView);
        if (useSwipeRefreshLayout()) {
            mSwipeRefresh.setRefreshing(false);
        }
    }

    public View getContentView() {
        return mContentView;
    }

    /**
     * 显示页面内部loading提示
     */
    public void showInPageLoadingView() {
        if (mContainerLayout.getChildCount() > 0) {
            mContainerLayout.removeAllViews();
        }
        getLayoutInflater().inflate(R.layout.loading_view, mContainerLayout);
    }

    public void showErrorView(@Nullable String errorMsg) {
        showErrorView(errorMsg, R.mipmap.ic_no_network);
    }

    public void showErrorView(@Nullable String pErrorMes, @DrawableRes int pErrorIconRes) {
        if (useSwipeRefreshLayout()) {
            mSwipeRefresh.setRefreshing(false);
        }
    }

    public void setScrollView(View scrollView) {
        this.mContainerLayout.setScrollView(scrollView);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mUnbinder.unbind();
    }

}
